package lv.imho.client.view;

import lv.imho.client.model.DataTransferObject;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Element of text list.
 * 
 * @author Igors Gulbinskis
 */
public class TextComponent extends VerticalPanel {
	
	private final String WIDTH = "100%";
	
	private final String TB_HEIGHT = "160px";
	private final String TB_WIDTH  = "100%";
	
	private DataTransferObject data;
	
	public TextComponent(DataTransferObject data) {
		super();
		this.data = data;
		
		setWidth(WIDTH);
		DOM.setStyleAttribute(getElement(), "border", "solid 2px #C3D9FF");
		DOM.setStyleAttribute(getElement(), "padding", "5px");
		DOM.setStyleAttribute(getElement(), "background", "#ffffff");
		initChildComponents();
	}
	
	private void initChildComponents() {
		Anchor title = new Anchor(data.getHeading(), data.getDataSource().getFeedUrl());		
		add(title);
		
		HTML bodyTextBox = new HTML(data.getHTML());
		bodyTextBox.setWidth(TB_WIDTH);
		bodyTextBox.setHeight(TB_HEIGHT);		
		DOM.setStyleAttribute(bodyTextBox.getElement(), "overflowY", "scroll");
		DOM.setStyleAttribute(bodyTextBox.getElement(), "marginTop", "5px");
		DOM.setStyleAttribute(bodyTextBox.getElement(), "marginBottom", "5px");
		add(bodyTextBox);
		
		Double evaluation = data.getEvaluation();
		if (evaluation == null) {
			evaluation = new Double(0);
		}
		
		HorizontalPanel evaluationPanel = new HorizontalPanel();
		Label evaluationLabel = new Label("Semantiskā orientācija:");
		DOM.setStyleAttribute(evaluationLabel.getElement(), "fontStyle", "italic");
		
		NumberFormat formatter = NumberFormat.getFormat("#.###");
		Label evaluationMark = new Label(formatter.format(data.getEvaluation()));
		
		DOM.setStyleAttribute(evaluationMark.getElement(), "fontStyle", "italic");
		DOM.setStyleAttribute(evaluationMark.getElement(), "fontWeight", "bold");
		if (evaluation > 0) {
			DOM.setStyleAttribute(evaluationMark.getElement(), "color", "green");
		} else if (evaluation < 0) {
			DOM.setStyleAttribute(evaluationMark.getElement(), "color", "red");
		} else {
			DOM.setStyleAttribute(evaluationMark.getElement(), "color", "blue");
		}
		
		Label spacer = new Label();
		spacer.setWidth("5px");
		
		evaluationPanel.add(evaluationLabel);
		evaluationPanel.add(spacer);
		evaluationPanel.add(evaluationMark);
		add(evaluationPanel);
	}
	
}
